package com.hlxl.viewpointrest.service;

import com.hlxl.viewpoint.model.ViewPoint;
import com.hlxl.viewpoint.service.ViewPointLocalServiceUtil;
import com.hlxl.viewpointrest.dto.Page;
import com.hlxl.viewpointrest.dto.ViewPointDTO;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wyy on 18-5-4
 */
@Service
@Transactional
public class ViewPointService {

    public List<ViewPoint> getViewPoints(String name, Page page) throws SystemException {
        return ViewPointLocalServiceUtil.getViewPoint(name, page.getStart(), page.getEnd());
    }

    public ViewPoint addViewPoint(ViewPointDTO viewPointDTO) throws SystemException {
        long id = CounterLocalServiceUtil.increment();
        ViewPoint viewPoint = ViewPointLocalServiceUtil.createViewPoint(id);
        viewPoint.setName(viewPointDTO.getName());
        ViewPointLocalServiceUtil.addViewPoint(viewPoint);
        return viewPoint;
    }

    public ViewPoint getViewPoint(long id) throws SystemException, PortalException {
        ViewPoint viewPoint = ViewPointLocalServiceUtil.getViewPoint(id);
        return viewPoint;
    }

    public void deleteViewPont(long id) throws SystemException, PortalException {
        ViewPointLocalServiceUtil.deleteViewPoint(id);

    }

    public ViewPoint updateViewPoint(ViewPointDTO viewPointDTO) throws SystemException, PortalException {
        ViewPoint viewPoint = ViewPointLocalServiceUtil.getViewPoint(viewPointDTO.getId());
        viewPoint.setName(viewPointDTO.getName());
        viewPoint = ViewPointLocalServiceUtil.updateViewPoint(viewPoint);
        return viewPoint;

    }
}
